/// FileName: search_bar
/// Author: admin
/// Date: 2021-05-15 19:31
/// Description: 一个普通的输入框

import 'package:flutter/material.dart';
import 'package:high_help_apps/common/values/define.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class CommonTextField extends StatefulWidget {

  CommonTextField({
    this.defaultText = '',
    this.padding,
    this.prefixIcon,
    this.placeholder,
    this.unselectBorderSide,
    this.selectBorderSide,
    this.backgroundColor,
    this.inputTextColor,
    this.placeholderColor,
    this.floatButtonBackgroundColor,
    this.floatButtonChild,
    required this.onPressed
  });

  /// 搜索框前面的图标
  final Icon? prefixIcon;

  /// 默认值
  final String? defaultText;

  /// 搜索框placeholder
  final String? placeholder;

  /// 搜索框未获取焦点的时候边框样式
  final BorderSide? unselectBorderSide;

  /// 搜索框获取焦点的时候边框样式
  final BorderSide? selectBorderSide;

  /// 背景颜色
  final Color? backgroundColor;

  /// 输入的文本颜色
  final Color? inputTextColor;

  /// placeholder颜色
  final Color? placeholderColor;

  /// 悬浮按钮的背景颜色
  final Color? floatButtonBackgroundColor;

  /// 悬浮按钮子组件
  final Widget? floatButtonChild;

  /// 点击了悬浮按钮或者按下了回车
  final sfunc onPressed;

  /// 输入框的内边距
  final EdgeInsets? padding;

  final TextEditingController textEditingController = TextEditingController();

  @override
  _CommonTextFieldState createState() => _CommonTextFieldState();
}

class _CommonTextFieldState extends State<CommonTextField> {


  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    widget.textEditingController.text = widget.defaultText ?? '';
  }


  @override
  Widget build(BuildContext context) {
    return Container(
      padding: widget.padding ?? EdgeInsets.all(10.r),
      child: buildTextField(context),
    );
  }

  TextField buildTextField(context) {
    return TextField(
      controller: widget.textEditingController,
      style: TextStyle(
          color: widget.inputTextColor
      ),
      decoration: InputDecoration(
        filled: true,
        fillColor: widget.backgroundColor,
        hintText: widget.placeholder,
        hintMaxLines: 1,
        hintStyle: TextStyle(
            color: widget.placeholderColor
        ),
        enabledBorder: OutlineInputBorder(
            borderSide: widget.unselectBorderSide ??
                BorderSide(
                    color: Theme.of(context).primaryColor,
                    width: 0.5
                )
        ),
        focusedBorder:  OutlineInputBorder(
            borderSide: widget.selectBorderSide ??
                BorderSide(
                    color: Theme.of(context).primaryColor,
                    width: 1
                )
        ),
        prefixIcon: widget.prefixIcon,
        suffixIcon: IconButton(icon: Icon(Icons.clear), color: widget.placeholderColor, onPressed: () {
          widget.textEditingController.clear();
          FocusScope.of(context).requestFocus(FocusNode());
        },),
      ),
      cursorColor: widget.placeholderColor,
      onSubmitted: widget.onPressed,
    );
  }
}